package org.hot100_0.leetcode34;

public class Solution {

    public static void main(String[] args) {
        int [] nums ={1,2,3,4,5,5,5,6};
        for (int i : searchRange(nums, 5)) {
            System.out.println(i);
        }

    }

    public static int[] searchRange(int[] nums, int target) {
        int[] res = new int[2];
        int leftIndex = findNum(nums, target);
        int rightIndex = findNum(nums,target+1)-1;
        if (leftIndex==nums.length || nums[leftIndex]!=target){
            res[0]=-1;
            res[1]=-1;
        }else {
            res[0]=leftIndex;
            res[1]=rightIndex;
        }
        return res;
    }

    private static int findNum(int[] nums, int target) {

        int left = 0;
        int right = nums.length-1;

        while (left <= right) {
            int mid = (left + right) >>> 1;

            if (target <= nums[mid]) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }

        return left;
    }
}
